#pragma once
#include <unordered_map>
#include <sstream>
#include <jsoncpp/json/json.h>
#include <memory>
#include "log.hpp"
class JsonUtil{
public:
    static bool deserialization(std::string& data,Json::Value& reg_info){
        Json::CharReaderBuilder crb; //创建器
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader()); //创建一个读
        std::string errs;
        bool ret = cr->parse(data.c_str(),data.c_str()+data.size(),&reg_info,&errs); //解析json
        if(ret){
            //成功
            logmessage(NORMAL,"反序列化成功");
            return true;
        }else{
            //失败
            logmessage(ERROR,"反序列化失败");
            return false;
        }
    }
    static bool serialization(std::string& data,Json::Value& reg_info){
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;
        int ret = sw->write(reg_info,&ss);
        if(ret != 0){
            logmessage(ERROR,"序列化失败");
            return false;
        }
        data = ss.str();
        return true;
    }
};

struct Userinfos{
    //id,nickname,score,total_cont,win_cont
    int id,score,total_cont,win_cont;
    std::string nickname;
};